// pages/people/information/information.js
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    slogan: '坚持就是胜利',
    age:'0',
    height:'0.00',
    originWeight:'0',
    myTarget:'减脂',
    targetWeight:'0'
  },
  getPlanText (code) {
    let plan = ['增肌', '减脂']
    return plan[code]
  },
  onLoad: function (options) {

    this.setData({
      slogan: app.globalData.userObject.slogan,
      age: app.globalData.userObject.age,
      height: app.globalData.userObject.height,
      originWeight: app.globalData.userObject.beginWeight,
      myTarget: app.globalData.userObject.healthyTarget,
      targetWeight: app.globalData.userObject.targetWeight,
    })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    }
  },
  bindinputSlogan(e) {
    this.setData({
      slogan: e.detail.value
    })
  },
  bindinputAge(e) {
    this.setData({
      age: e.detail.value
    })
  },
  bindinputHeight(e) {
    this.setData({
      height: e.detail.value
    })
  },
  bindinputOriginWeight(e) {
    this.setData({
      originWeight: e.detail.value
    })
  },
  bindinputMyTarget(e) {
    this.setData({
      myTarget: e.detail.value
    })
  },
  bindinputTargetWeight(e) {
    this.setData({
      targetWeight: e.detail.value
    })
  },
  saveUserInfo() {
    // 保存数据服务端
    let userObject = app.globalData.userObject
    userObject.slogan = this.data.slogan
    userObject.age = this.data.age
    userObject.height = this.data.height
    userObject.beginWeight = this.data.originWeight
    userObject.healthyTarget = this.data.myTarget
    userObject.targetWeight = this.data.targetWeight

    app.apis().updateUser(app.globalData.userObject.id, userObject, function (res) {
      app.globalData.userObject = res.data
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000
      })
    })
  }
})